package sqlist;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created with IntelliJ IDEA
 * Description:扑克牌
 * User：chenshu
 * Date：2023-10-28
 * Time：21:50
 */
class Card {
    public String suit;
    public int rank;

    public Card(){
        suit = "";
        rank = 0;
    }

    public Card(String suit, int rank) {
        this.suit = suit;
        this.rank = rank;
    }

    @Override
    public String toString() {
        return String.format("[%s %d]", suit, rank);
    }
}

class DeckMethod {
    public static final String[] suits = {"♠", "♥", "♣", "♦"};
    //买一副牌
    public static List<Card> buyDeck() {
        List<Card> deck = new ArrayList<>(52);

        for (int i = 0; i < 4; i++) {
            for (int j = 1; j <= 13; j++) {
                Card card = new Card(suits[i], j);
                deck.add(card);
            }
        }
        return deck;
    }

    public static void swap(List<Card> deck, int left, int right) {
        Card tmp = deck.get(left);
        deck.set(left, deck.get(right));
        deck.set(right, tmp);
    }
    //洗牌
    public static void shuffle(List<Card> deck) {
        Random random = new Random();
        for (int i = deck.size() - 1; i > 0 ; i--) {
            int index = random.nextInt(i);
            swap(deck, index, i);
        }
    }
}
public class TestCard {
    public static void main(String[] args) {
        List<Card> deck = DeckMethod.buyDeck();
        System.out.println("买一副牌：" + deck);
        DeckMethod.shuffle(deck);
        System.out.println("荷官洗牌：" + deck);

        //三个玩家
        List<Card> player1 = new ArrayList<>();
        List<Card> player2 = new ArrayList<>();
        List<Card> player3 = new ArrayList<>();
        //
        List<List<Card>> game = new ArrayList<>();
        game.add(player1);
        game.add(player2);
        game.add(player3);
        //三个人轮流摸一张牌，摸五次
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 3; j++) {
                List<Card> player = game.get(j);
                Card card = deck.remove(0);
                player.add(card);
            }
        }
        System.out.println("第1个人的牌" + game.get(0));
        System.out.println("第2个人的牌" + game.get(1));
        System.out.println("第3个人的牌" + game.get(2));
        System.out.println("剩余的牌" + deck);
    }
}
